package com.shijunhao.graduation.controller;


import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.BusinessForumThymeDto;
import com.shijunhao.graduation.domain.dto.BusinessForumTypeDto;
import com.shijunhao.graduation.domain.entity.BusinessForumThyme;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessForumThymeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/business-forum-thyme")
@Api(tags = "论坛主题")
public class BusinessForumThymeController {
    @Resource
    private IBusinessForumThymeService businessForumThymeService;

    @PostMapping("/admin/insert")
    @ApiOperation("新增")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.INSERT,description = "新增论坛主题")
    public ResultCode<Long> insert(@RequestBody BusinessForumThymeDto dto){
        Long insert = businessForumThymeService.insert(dto);
        if(insert != null)
            return ResultCode.success("插入成功",insert);
        else
            return ResultCode.error("插入失败");
    }

    @PostMapping("/admin/update")
    @ApiOperation("更新")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.UPDATE,description = "更新主题")
    public ResultCode<Boolean> update(@RequestBody BusinessForumThymeDto dto){
        Boolean update = businessForumThymeService.update(dto);
        if(update)
            return ResultCode.success("更新成功");
        else
            return ResultCode.error("更新失败");
    }

    @PostMapping("/admin/delete/{id}")
    @ApiOperation("删除")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.DELETE,description = "删除主题")
    public ResultCode<Boolean> delete(@PathVariable Long id){
        boolean delete = businessForumThymeService.deleteById(id);
        if(delete)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }
}
